﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoreHelper.Validation.Validators
{
    public class CustomValidatorAttribute : BaseValidatorAttribute
    {
        public CustomValidatorAttribute(string funKey) : this(funKey, ValidationUtils.Messages.Custom)
        {
        }
        public CustomValidatorAttribute(string funKey, string errorMessage) : base(errorMessage)
        {
            FunKey = funKey;
        }

        public string FunKey { get; protected set; }

        public override bool IsValid(object value, ValidationContext validationContext)
        {
            Func<object, ValidationContext, bool> fun;
            if (Validator.m_customValidators.TryGetValue(FunKey, out fun))
            {
                return fun(value, validationContext);
            }
            return true;
        }

    }
}
